package top.zhaohaibo.expandable;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;

import top.zhaohaibo.expandable.adapter.MyExpandableAdapter;
import top.zhaohaibo.expandable.mode.ExpandableMode;

/**
 * 作者: Ocean<br><br>
 * 时间: 2020/3/28 9:57 PM<br><br>
 * 邮箱: xinzhaohaibo@aliyun.com<br><br>
 * 描述: git pull origin master --allow-unrelated-histories
 */
public class MainActivity extends AppCompatActivity {

    /** 数据集合 */
    private List<ExpandableMode> mExpandableModeList;
    /** 适配器 */
    private MyExpandableAdapter mExpandableAdapter;
    /** 可展开的List */
    private ExpandableListView mExpandableListView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mExpandableListView = findViewById(R.id.act_main_expandable_list_view);

        // 调用readAssetsFile方法读取assets下的测试数据
        String ExpandableModeStr = readAssetsFile("ExpandableMode.json", this);
        Gson gson = new Gson();
        // 通过Gson把读到的ExpandableModeStr转换为实体对象
        mExpandableModeList = gson.fromJson(ExpandableModeStr, new TypeToken<List<ExpandableMode>>() {
        }.getType());
        // 实例化适配器
        mExpandableAdapter = new MyExpandableAdapter(mExpandableModeList, this);
        // 设置适配器
        mExpandableListView.setAdapter(mExpandableAdapter);

        // 子item的监听
        mExpandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                Toast.makeText(MainActivity.this, childPosition + "", Toast.LENGTH_SHORT).show();
                List<ExpandableMode.ChildDataBean> childDataBeans = mExpandableModeList.get(groupPosition).getChildData();
                childDataBeans.add(childDataBeans.get(childPosition));
                // 设置数据
                mExpandableAdapter.setExpandableModeList(mExpandableModeList);
                // 通过关闭组item在展开刷新数据
                mExpandableListView.collapseGroup(groupPosition);
                mExpandableListView.expandGroup(groupPosition);
                return false;
            }
        });

        // 打开所有
        for (int i = 0; i < mExpandableModeList.size(); i++) mExpandableListView.expandGroup(i);
        // 设置默认展开蔬菜组
        // mExpandableListView.expandGroup(1);
    }


    /**
     * 读取assets中的文件 流程
     * readAssetsFile("json/xx.json", this);
     *
     * @param fileName 文件名
     * @param context  context
     * @return 读取的内容
     */
    public String readAssetsFile(String fileName, Context context) {
        // 输入流对象
        InputStream inputStream;
        // 缓存流对象
        BufferedReader bufferedReader = null;
        StringBuilder stringBuilder = new StringBuilder();
        try {
            // getAssets方法返回通过输入流对象
            inputStream = context.getAssets().open(fileName);
            // InputStreamReader 实现字节流到字符流的转换
            bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                stringBuilder.append(line);
            }
        } catch (IOException e) {
            Log.e("MainActivity", e.toString());
        } finally {
            try {
                if (bufferedReader != null) {
                    bufferedReader.close();
                }
            } catch (IOException e) {
                Log.e("MainActivity", e.toString());
            }
        }
        return stringBuilder.toString();
    }
}
